package com.gdkj.ip;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.gdkj.common.LogicResult;
import com.gdkj.constant.CodeConstants;
import com.gdkj.constant.CodeMessage;
import com.gdkj.constant.RedisConstant;
import com.gdkj.entity.IpDeviceBlack;
import com.gdkj.entity.live.CmfWhiteList;
import com.gdkj.enums.BlackTypeEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class IpService implements IIp {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public LogicResult authIpAndDevice(String ip, String deviceOne, String deviceTwo) {
        //log.info("ip地址为:{},设备id为:{},{}", ip, deviceOne, deviceTwo);
        // 1. 查询缓存数据
        String allIpDeviceBlackInfoStr = stringRedisTemplate.opsForValue().get(RedisConstant.IP_DEVICE_BLACK_LIST_KEY);
        //log.info("设备缓存数据为:{}", allIpDeviceBlackInfoStr);
        if (StrUtil.isEmpty(allIpDeviceBlackInfoStr)) {
            return LogicResult.success();
        }

        List<IpDeviceBlack> ipDeviceBlackList = JSONObject.parseArray(allIpDeviceBlackInfoStr, IpDeviceBlack.class);
        if (ipDeviceBlackList == null || ipDeviceBlackList.size() == 0) {
            return LogicResult.success();
        }

        // 2. 循环验证是否命中设备id和ip
        for (IpDeviceBlack ipDeviceBlack : ipDeviceBlackList) {
            // 2.1 判断ip
            if (StrUtil.isNotEmpty(ip) && BlackTypeEnums.IP.getType().equals(ipDeviceBlack.getType()) && ip.equals(ipDeviceBlack.getValue())) {
                return new LogicResult(CodeConstants.HIT_IP_BLACK_LIST, null, CodeMessage.ERROR_CODE.get(CodeConstants.HIT_IP_BLACK_LIST));
            }

            // 2.2 判断设备id
            if (StrUtil.isNotEmpty(deviceOne) && BlackTypeEnums.BLACK.getType().equals(ipDeviceBlack.getType()) && deviceOne.equals(ipDeviceBlack.getValue())) {
                return new LogicResult(CodeConstants.HIT_DEVICE_BLACK_LIST, null, CodeMessage.ERROR_CODE.get(CodeConstants.HIT_DEVICE_BLACK_LIST));
            }

            if (StrUtil.isNotEmpty(deviceTwo) && BlackTypeEnums.BLACK.getType().equals(ipDeviceBlack.getType()) && deviceTwo.equals(ipDeviceBlack.getValue())) {
                return new LogicResult(CodeConstants.HIT_DEVICE_BLACK_LIST, null, CodeMessage.ERROR_CODE.get(CodeConstants.HIT_DEVICE_BLACK_LIST));
            }
        }

        return LogicResult.success();
    }

    /**
     * 检查用户白名单
     * @param uid
     * @return boolean
     */
    @Override
    public boolean checkUserWhite(Long uid) {
        String cacheStr = stringRedisTemplate.opsForValue().get(RedisConstant.WHITE_LIST_KEY);
        if (StrUtil.isEmpty(cacheStr)) {
            return false;
        }

        List<CmfWhiteList> dataList = JSONObject.parseArray(cacheStr, CmfWhiteList.class);
        if (CollUtil.isEmpty(dataList)) {
            return false;
        }
        for (CmfWhiteList cmfWhiteList : dataList) {
            if (cmfWhiteList.getValue().equals(uid.toString())) {
                return true;
            }
        }

        return false;
    }
}
